package com.woniuxy.common.api;

import com.woniuxy.common.api.enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 公共响应结果
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CommonResult<T> {

    private Integer code;

    private String message;

    private T data;

    public static <T> CommonResult success(T data) {
        return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 返回成功结果
     *
     * @param message 自定义消息
     * @param data    响应数据
     * @param <T>
     * @return 200状态码
     */
    public static <T> CommonResult success(String message, T data) {
        return new CommonResult(ResultCode.SUCCESS.getCode(), message, data);
    }

    public static <T> CommonResult success(Integer code, String message, T data) {
        return new CommonResult(code, ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T> CommonResult failure() {
        return new CommonResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), null);
    }

    public static <T> CommonResult failure(Integer code, String message) {
        return new CommonResult(code, message, null);
    }

    public static <T> CommonResult failure(IErrorCode errorCode) {
        return new CommonResult(errorCode.getCode(), errorCode.getMessage(), null);
    }

}
